package com.ruoyi.web.controller.production;

import java.util.List;

import com.ruoyi.mes.production.domain.SmesTechProcedure;
import com.ruoyi.mes.production.service.ISmesTechProcedureService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.production.domain.SmesTechProcess;
import com.ruoyi.mes.production.service.ISmesTechProcessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺流程管理Controller
 * 
 * @author simpsense
 * @date 2020-12-02
 */
@RestController
@RequestMapping("/production/techProcess")
public class SmesTechProcessController extends BaseController
{
    @Autowired
    private ISmesTechProcessService smesTechProcessService;
    @Autowired
    private ISmesTechProcedureService smesTechProcedureService;
    /**
     * 查询工艺流程管理列表
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmesTechProcess smesTechProcess)
    {
        startPage();
        List<SmesTechProcess> list = smesTechProcessService.selectSmesTechProcessList(smesTechProcess);
        return getDataTable(list);
    }

    /**
     * 导出工艺流程管理列表
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:export')")
    @Log(title = "工艺流程管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesTechProcess smesTechProcess)
    {
        List<SmesTechProcess> list = smesTechProcessService.selectSmesTechProcessList(smesTechProcess);
        ExcelUtil<SmesTechProcess> util = new ExcelUtil<SmesTechProcess>(SmesTechProcess.class);
        return util.exportExcel(list, "techProcess");
    }

    /**
     * 获取工艺流程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:query')")
    @GetMapping(value = "/{techProcessNo}")
    public AjaxResult getInfo(@PathVariable("techProcessNo") String techProcessNo)
    {
        return AjaxResult.success(smesTechProcessService.selectSmesTechProcessById(techProcessNo));
    }

    /**
     * 新增工艺流程管理
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:add')")
    @Log(title = "工艺流程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesTechProcess smesTechProcess)
    {
        return toAjax(smesTechProcessService.insertSmesTechProcess(smesTechProcess));
    }

    /**
     * 修改工艺流程管理
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:edit')")
    @Log(title = "工艺流程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesTechProcess smesTechProcess)
    {
        return toAjax(smesTechProcessService.updateSmesTechProcess(smesTechProcess));
    }

    /**
     * 删除工艺流程管理
     */
    @PreAuthorize("@ss.hasPermi('production:techProcess:remove')")
    @Log(title = "工艺流程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{techProcessNos}")
    public AjaxResult remove(@PathVariable String[] techProcessNos)
    {
        for (String techProcessNo:techProcessNos
             ) {
            SmesTechProcedure procedure=new SmesTechProcedure();
            procedure.setTechProcessNo(techProcessNo);
            List<SmesTechProcedure> procedures= smesTechProcedureService.selectSmesTechProcedureList(procedure);
            if(procedures!=null && procedures.size()>0){
                return AjaxResult.error(techProcessNo+"存在工艺流程数据,不允许删除");
            }
        }
        return toAjax(smesTechProcessService.deleteSmesTechProcessByIds(techProcessNos));
    }
}
